package homework.six.UDP;

import java.net.*;

public class Server {
    public static void main(String[] args) {
        try {
            InetAddress ip = InetAddress.getLocalHost();
            int port = 8001;
            DatagramSocket getSocket = new DatagramSocket(port, ip);
            byte[] buf = new byte[1024];
            DatagramPacket getPacket = new DatagramPacket(buf, buf.length);
            getSocket.receive(getPacket);
            String getMes = new String(buf, 0, getPacket.getLength());
            System.out.println("对方发送的消息：" + getMes);
            InetAddress sendIP = getPacket.getAddress();
            int sendPort = getPacket.getPort();
            System.out.println("对方的地址是：" + sendIP.getHostAddress() + "：" + sendPort);
            SocketAddress sendAddress = getPacket.getSocketAddress();
            String feedback = "接收方：收到了！";
            byte[] backbuf = feedback.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(backbuf, backbuf.length, sendAddress);
            getSocket.send(sendPacket);
            getSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
